var e = getApp(), a = e.requirejs("core"), t = e.requirejs("biz/diyform");

Page({
  data: {
    bankIndex: 0,
    cateIndex: 0,
    tempFilePaths: "",
    images: [],
    reg: [],
    imgs: []
  },
  onLoad: function (e) {
    this.getData();
  },
  onShow: function () {

  },
  getData: function () {
    var t = e.getCache("orderAddress"), i = this;
    a.get("merch/register", {}, function (e) {
      console.log(e.error), 70015 != e.error ? (e.show = !0, wx.setNavigationBarTitle({
        title: "申请店铺"
      }), i.setData(e), t && i.setData({
        address: t,
        bankIndex: 0,
        cateIndex: 0,
      }), i.setData({
        diyform: {
          f_data: e.f_data,
          fields: e.fields
        }
      })) : wx.redirectTo({
        url: "/pages/merch/index"
      });
    });
  },
  inputChange: function (e) {
    "merchname" == e.target.id ? this.setData({
      "reg.merchname": e.detail.value
    }) : "salecate" == e.target.id ? this.setData({
      "reg.salecate": e.detail.value
    }) : "desc" == e.target.id ? this.setData({
      "reg.desc": e.detail.value
    }) : "uname" == e.target.id ? this.setData({
      "reg.uname": e.detail.value
    }) : "upass" == e.target.id ? this.setData({
      "reg.upass": e.detail.value
    }) : "realname" == e.target.id ? this.setData({
      "reg.realname": e.detail.value
    }): "idcard" == e.target.id ? this.setData({
      "reg.idcard": e.detail.value
    }) : "mobile" == e.target.id && this.setData({
      "reg.mobile": e.detail.value
    }) ;
	// console.log(e.target.id);
	// console.log(e.detail.value);
	// console.log(this.data.reg);

  },
  bankChange: function (e) {
    var a = e.detail.value;
    this.setData({
      bankIndex: a
    });
  },
  cateChange: function (e) {
    var a = e.detail.value;
    this.setData({
      cateIndex: a
    });
  },
  upload: function (e) {
    var t = this, i = a.data(e), r = i.type, s = t.data.images, n = t.data.imgs, m = i.index;
    "image" == r ? a.upload(function (e) {
      s.push(e.filename), n.push(e.url), t.setData({
        images: s,
        imgs: n
      });
    }) : "image-remove" == r ? (s.splice(m, 1), n.splice(m, 1), t.setData({
      images: s,
      imgs: n
    })) : "image-preview" == r && wx.previewImage({
      current: n[m],
      urls: n
    });
  },
  submit: function (e) {
    var that=this;
    var i = this.data;
    if (0 == this.data.template_flag) {
      if (!this.data.reg.merchname) return void a.alert("请填写,店铺名称!");
      if (!this.data.reg.salecate) return void a.alert("请填写,主营项目!");
      if (!this.data.reg.realname) return void a.alert("请填写,身份证姓名!");
      if (!this.data.reg.idcard) return void a.alert("请填写,身份证号码!");
      if (!this.data.reg.mobile) return void a.alert("请填写,联系方式!");
      if (!this.data.reg.uname) return void a.alert("请填写,账号!");
      if (!this.data.reg.upass) return void a.alert("请填写,密码!");

      s = {
        realname: this.data.reg.realname,
        idcard: this.data.reg.idcard,
        mobile: this.data.reg.mobile,
        merchname: this.data.reg.merchname,
        salecate: this.data.reg.salecate,
        desc: this.data.reg.desc,
        uname: this.data.reg.uname,
        upass: this.data.reg.upass
      };
    }
	else {
      var r = this.data.diyform;
      t.verify(this, r) || a.alert("请检查必填项是否填写");
      var s = {
        memberdata: this.data.diyform.f_data,
		realname: this.data.reg.realname,
    idcard: this.data.reg.idcard,
        mobile: this.data.reg.mobile,
        merchname: this.data.reg.merchname,
        salecate: this.data.reg.salecate,
        desc: this.data.reg.desc,
        uname: this.data.reg.uname,
        upass: this.data.reg.upass
      };
    }
	console.log(s);
    a.post("merch/register", s, function (e) {
      if (e.status==1){
        a.alert('提交成功')
        that.onLoad();
      }else{
        a.alert(e.result.message)
      }
      /*0 != e.status ?  : wx.redirectTo({
        url: 1 == e.check ? "/pages/merch/index" : "/pages/merch/register/index"
      });*/
    });
  },
  DiyFormHandler: function (e) {
    return t.DiyFormHandler(this, e);
  }
});